import { configureStore, combineReducers } from '@reduxjs/toolkit';
import logger from 'redux-logger';
import appReducer from './reducers/appSlice';
import counterReducer from './reducers/counterSlice';
import headerReducer from './reducers/headerSlice';
import leftSiderReducer from './reducers/leftSiderSlice';
import breadReducer from './reducers/breadSlice';
import footerReducer from './reducers/footerSlice';

const rootReducer = combineReducers({
    app: appReducer,
    counter: counterReducer,
    header: headerReducer,
    leftSider: leftSiderReducer,
    bread: breadReducer,
    footer: footerReducer,
});

let middleware: any = (getDefaultMiddleware: () => any[]) => getDefaultMiddleware();

if (process.env.NODE_ENV == 'devlog') {
    middleware = (getDefaultMiddleware: () => any[]) => getDefaultMiddleware().concat(logger);
}

const store = configureStore({
    reducer: rootReducer,
    middleware: middleware,
    devTools: true,
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;
export default store;
